﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ditto.Models;

namespace Ditto.Controllers
{ 
    public class TransactionsController : Controller
    {
        DittoEntities db = new DittoEntities();
        ITransactionRepository transactionRepository;

        public TransactionsController()
            : this(new TransactionRepository())
        {
        }

        public TransactionsController(ITransactionRepository repository)
        {
            transactionRepository = repository;
        }

        //
        // GET: /Transactions/

        public ViewResult Index()
        {
            var transactions = db.Transactions.Include("Item").Include("User");
            return View(transactions.ToList());
        }

        //
        // GET: /Transactions/Details/5

        public ViewResult Details(int id)
        {
            Transaction transaction = db.Transactions.Single(t => t.TransactionID == id);
            return View(transaction);
        }

        [Authorize]
        public ActionResult AcceptRequest(int itemID, string requestingUserID)
        {
            ItemRepository itemRepository = new ItemRepository();
            Request request = itemRepository.FindRequest(itemID, requestingUserID);
            request.IsApproved = true;

            Transaction trans = Transaction.CreateTransaction(0, itemID, requestingUserID, DateTime.UtcNow, false);

            trans.IsPickedup = false;
            trans.IsReturned = false;

            trans.PickupAddress = request.Item.Address;
            trans.PickupTime = DateTime.UtcNow.AddDays(2);
            trans.PickupLongitude = request.Item.Longitude;
            trans.PickupLatitude = request.Item.Latitude;

            if (request.Item.ReturnDate.HasValue)
            {
                trans.ReturnAddress = request.Item.Address;
                trans.ReturnTime = request.Item.ReturnDate;
                trans.ReturnLongitude = request.Item.Longitude;
                trans.ReturnLatitude = request.Item.Longitude;
            }

            try
            {
                transactionRepository.Add(trans);
                transactionRepository.Save();

                itemRepository.Save();

                return View("Details", trans.TransactionID);  
            }
            catch (Exception ex)
            {
                throw ex;
            }


            //create new transaction

            //approve request

            //deny other requests? (maybe, think about this, esp if transaction is cancelled)

            //mark item as not available

            //item details remove request status and replace with transaction status


            //

        }

        //
        // GET: /Transactions/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            Transaction trans = transactionRepository.GetTransaction(id);
            return View(trans);
        }

        //
        // POST: /Transactions/Edit/5

        [HttpPost, Authorize]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Transaction trans = transactionRepository.GetTransaction(id);

            try
            {

                UpdateModel(trans);

                transactionRepository.Save();

                return RedirectToAction("Details", new { id = id });
            }
            catch
            {
                return View(trans);
            }
        }

        //
        // GET: /Transactions/Delete/5
 
        public ActionResult Delete(int id)
        {
            Transaction transaction = db.Transactions.Single(t => t.TransactionID == id);
            return View(transaction);
        }

        //
        // POST: /Transactions/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Transaction transaction = db.Transactions.Single(t => t.TransactionID == id);
            db.Transactions.DeleteObject(transaction);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}